﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.EntitiesInfo;
using ConvertUtility = Lib.Utility.ConvertUtility;

namespace Web.Cms.FootballPlayer
{
    public partial class CreateEditFootballPlayer1 : AdminUserControl
    {
        #region Khai báo
        private int footBallId;
        private CMSFootballPlayerInfo info;
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                footBallId = ConvertUtility.ToInt32(Request.QueryString["footBallId"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["footBallId"]);               

                txtTeaser.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));

                if (!Page.IsPostBack)
                {
                    //Fill dữ liệu thuộc câu lạc bộ                  
                    CMSClubName.FillDropList(dropClubId, "0");                    
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (footBallId > 0)
                {
                    btnUpdateFootballPlayer.Visible = true;
                    btnAddFootballPlayer.Visible = false;

                    info = CMSFootballPlayer.GetInfo(footBallId);
                    if (info != null)
                    {
                        txtFullName.Text = info.FullName;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtDateBrithday.SelectedDate = info.DateBrithday;
                        txtTeaser.Text = info.Teaser;
                        txtAvatar.Text = info.Avatar;
                        txtBody.Text = info.Contents;
                        dropClubId.SelectedValue = info.ClubId.ToString();
                        dropZoneId.SelectedValue = info.NationalId.ToString();



                    }
                }
                else
                {
                    txtDateBrithday.SelectedDate = DateTime.Now;
                    btnUpdateFootballPlayer.Visible = false;
                    btnAddFootballPlayer.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(CMSFootballPlayerInfo info)
        {

            info.FootBallId = footBallId;
            info.FullName = txtFullName.Text;
            info.Teaser = txtTeaser.Text;
            info.Contents = txtBody.Text;
            info.Status = 1;
            info.ShowType = 0;
            info.DateBrithday = txtDateBrithday.SelectedDate;
            info.ClubId = ConvertUtility.ToInt32(dropClubId.SelectedValue);
            info.NationalId = 0;
            info.Avatar = txtAvatar.Text.Trim();
        }
        #endregion

        #region thêm mới cầu thủ

        protected void btnAddFootballPlayer_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSFootballPlayerInfo();
                FillData(info);
                if (txtFullName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên cầu thủ?");
                    txtFullName.Focus();
                    return;
                }
                else
                {
                    int i = CMSFootballPlayer.Insert(info);
                    if (i > 0)
                    {
                        Response.Redirect("/cms/Default.aspx?ctrlkey=ListFootballPlayerPublic");                     

                    }
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }

        }
        #endregion

        #region sửa thông tin cầu thủ
        protected void btnUpdateFootballPlayer_Click(object sender, EventArgs e)
        {
            try
            {
                info = new CMSFootballPlayerInfo();
                FillData(info);
                if (txtFullName.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tên cầu thủ?");
                    txtFullName.Focus();
                    return;
                }
                else
                {
                    CMSFootballPlayer.Update(info);
                    Response.Redirect("/cms/Default.aspx?ctrlkey=ListFootballPlayerPublic");
                   
                }

            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region thoát

        protected void btnCanelFootballPlayer_Click(object sender, EventArgs e)
        {
            Response.Redirect("/cms/Default.aspx?ctrlkey=ListFootballPlayerPublic");
        }

        public void ClearText()
        {
            txtFullName.Text = "";
            txtTeaser.Text = "";
            txtBody.Text = "";
            
        }
        #endregion
    }
}